package top.laoshuzi.android.mvp.compiler.processor;

import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.JavaFile;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.TypeSpec;
import com.squareup.javapoet.TypeVariableName;

import java.io.IOException;
import java.util.Set;

import javax.annotation.processing.Filer;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.Elements;

@Deprecated
public class PresenterProcessor implements IProcessor {

    private Elements elementUtils;
    private Filer filer;

    public PresenterProcessor(ProcessingEnvironment processingEnv) {
        elementUtils = processingEnv.getElementUtils();
        filer = processingEnv.getFiler();
    }

    @Override
    public void process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {

        TypeSpec.Builder classBuilder = TypeSpec.classBuilder("Presenter")
                .addModifiers(Modifier.PUBLIC)
                .addTypeVariable(TypeVariableName.get("View"))
                .addSuperinterface(ClassName.bestGuess("top.laoshuzi.android.mvp.presenter.PresenterLifecycle"));

        MethodSpec.Builder onCreateMethodBuilder = MethodSpec.methodBuilder("onCreate")
                .addAnnotation(Override.class)
                .addModifiers(Modifier.PUBLIC);

        MethodSpec.Builder onDestroyMethodBuilder = MethodSpec.methodBuilder("onDestroy")
                .addAnnotation(Override.class)
                .addModifiers(Modifier.PUBLIC);

        String packageName = "top.laoshuzi.mvp";
        classBuilder.addMethod(onCreateMethodBuilder.build())
                .addMethod(onDestroyMethodBuilder.build());
        try {
            JavaFile.builder(packageName, classBuilder.build())
                    .addFileComment("Automatically generated MVP code, do not modify!!!")
                    .build()
                    .writeTo(filer);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
